package com.hfh.net;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author hfh
 * @Date 2022 09 13 13 59
 **/
public class Test4 {
    @Test
    public void server() throws IOException {
        ServerSocket ss = new ServerSocket(9999);

        Socket s =ss.accept();
        InputStream is = s.getInputStream();
        FileOutputStream fos = new FileOutputStream("b.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] arr = new byte[1024];
        int len;
        while ((len= is.read(arr))!=-1){
            bos.write(arr,0,len);
        }

        bos.close();
        fos.close();
        s.close();
        ss.close();





    }

    @Test
    public void client() throws IOException {
        Socket s = new Socket(InetAddress.getLocalHost(),9999);

        FileInputStream fis = new FileInputStream("a.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = s.getOutputStream();

        byte[] arr = new byte[1024];
        int len;
        while ((len= bis.read(arr))!=-1){
            os.write(arr,0,len);
        }

//        InputStream is = s.getInputStream();
//        byte[] arr1 = new byte[1024];
//        int len1;
//        while ((len1= is.read(arr1))!=-1){
//            System.out.println(new String(arr, 0, len1));
//        }

        os.close();
        bis.close();
        fis.close();
        s.close();
    }
}
